﻿''' <summary>
''' 符合JOS习惯的纯字符串字典结构。
''' </summary>
Public Class JosDictionary
    Inherits Dictionary(Of String, String)
    Private Const DATE_TIME_FORMAT As String = "yyyy-MM-dd HH:mm:ss"

    Public Sub New()
    End Sub

    Public Sub New(ByVal dictionary As IDictionary(Of String, String))
        MyBase.New(dictionary)
    End Sub

    ''' <summary>
    ''' 添加一个新的键值对。空键或者空值的键值对将会被忽略。
    ''' </summary>
    ''' <param name="key">键名称</param>
    ''' <param name="value">键对应的值</param>
    Public Shadows Sub Add(ByVal key As String, ByVal value As Object)
        Dim strValue As String = ValueToString(value)
        Me.Add(key, strValue)
    End Sub

    ''' <summary>
    ''' 将键对应的值转化为String
    ''' </summary>
    ''' <param name="value">键对应的值，目前支持：string, int, long, double, bool, DateTime类型</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Shared Function ValueToString(ByVal value As Object) As String
        Dim strValue As String

        If value Is Nothing Then
            strValue = Nothing
        ElseIf TypeOf value Is String Then
            strValue = DirectCast(value, String)
        ElseIf TypeOf value Is Nullable(Of DateTime) Then
            Dim dateTime As Nullable(Of DateTime) = DirectCast(value, Nullable(Of DateTime))
            strValue = dateTime.Value.ToString(DATE_TIME_FORMAT)
        ElseIf TypeOf value Is Nullable(Of Integer) Then
            strValue = DirectCast(value, Nullable(Of Integer)).Value.ToString()
        ElseIf TypeOf value Is Nullable(Of Long) Then
            strValue = DirectCast(value, Nullable(Of Long)).Value.ToString()
        ElseIf TypeOf value Is Nullable(Of Double) Then
            strValue = DirectCast(value, Nullable(Of Double)).Value.ToString()
        ElseIf TypeOf value Is Nullable(Of Boolean) Then
            strValue = DirectCast(value, Nullable(Of Boolean)).Value.ToString().ToLower()
        Else
            strValue = value.ToString()
        End If

        Return strValue
    End Function

    Public Shadows Sub Add(ByVal key As String, ByVal value As String)
        If Not String.IsNullOrEmpty(key) AndAlso Not String.IsNullOrEmpty(value) Then
            MyBase.Add(key, value)
        End If
    End Sub

End Class